package net.sky.oa.util;

import java.io.File;
import java.io.IOException;

/**
 * 将指定目录及其子目录下的js文件压缩成gzjs格式
 */
public class FileCompressUtilTest {
	public static void main(String[] args) throws IOException, InterruptedException {
		java.io.File file = new java.io.File(
				"d:\\update");
		gzip(file);
		Thread.sleep(5000);
		rename(file);
	}
	//gzip All the js files
	private static void gzip(File file) throws IOException{
		if (file.isDirectory()) {
			Process process =Runtime.getRuntime().exec("F:\\Software\\Programing\\gzip -9 "+file.getAbsolutePath()+"/*.js");
			File[] files = file.listFiles();
			if(null!=file){
				for (File fileFrom : files) {
					gzip(fileFrom);
				}
			}
		}
	}
	//rename .js.gz files to .gzjs files
	public static void rename(File file) throws IOException {
		String dirPath = file.getAbsolutePath();// 目录路径
		if (file.isDirectory()) {
			File[] files = file.listFiles();// 获取此目录下的文件列表
			for (File fileFrom : files) {
				String fromFile = fileFrom.getName();// 文件名
				String toFileName;
				if (fromFile.toLowerCase().endsWith(".js.gz")) {
					int index = fromFile.lastIndexOf(".js.gz");
					toFileName = dirPath + "\\" + fromFile.substring(0, index)
							+ ".gzjs";
					File toFile = new File(toFileName);
					if (fileFrom.exists() && !toFile.exists()) {
						fileFrom.renameTo(toFile);
					}
				}
				rename(fileFrom);
			}
		}

	}
}